package com.example.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.reggie.common.CustomException;
import com.example.reggie.dto.DishDto;
import com.example.reggie.entity.Dish;
import com.example.reggie.entity.DishFlavor;
import com.example.reggie.mapper.DishMapper;
import com.example.reggie.service.DishFlavorService;
import com.example.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 老肥！！！
 * @Date: 2022/09/14/20:26
 * @Description: 不想学代码了！！！
 */
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private  DishService dishService;
    //    新增菜品，同时保存对应的口味数据
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //      保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        Long dishid = dishDto.getId(); //菜品id
//        菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
//        flavors = flavors.stream().map((item) -> {
//            item.setDishId(dishid);
//            return item;
//        }).collect(Collectors.toList());
        for (DishFlavor dishFlavor : flavors) {
            dishFlavor.setDishId(dishid);
        }
//          保存菜品口味数据到菜品口味表 dish_flavor
        dishFlavorService.saveBatch(dishDto.getFlavors());
    }
    @Override
    public DishDto getByIdWithFlavor(Long id){
        //查询菜品基本信息，从dish表查询
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);//粗略理解：将dish所有属性都复制到Dto里，减少要敲的代码量

        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors =  dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);
        return dishDto;
    }
    @Override
    public void updateWithFlavor(DishDto dishDto){
// 更新dish表的基本信息
        this.updateById(dishDto);
//        清理当前菜品对应的口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
//        添加当前提交的口味数据---dish_flavor表的insert操作
        List<DishFlavor>flavors = dishDto.getFlavors();
        for (DishFlavor dishFlavor : flavors) {
            dishFlavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void batchDeleteByids(List<Long> ids) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ids!=null,Dish::getId,ids);
        List<Dish> list = this.list(queryWrapper);

        if(list != null) {
            for(Dish dish : list){
                if(dish.getStatus()==1){
                    throw new CustomException("该商品正在售卖,不可删除");
                }else{
                   this.removeByIds(ids);
                }
            }
        }
    }

    @Override
    public boolean batchUpdateStatu(Integer status, List<Long> ids) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ids != null,Dish::getId,ids);
        List<Dish> list = this.list(queryWrapper);

        if(list != null){
            for(Dish dish : list){
                dish.setStatus(status);//修改菜品的售卖状态
                this.updateById(dish);
            }
            return true;
        }else{
            return false;
        }
    }
}
